<template>
	<view class="page">
		<view class="info-box">
			<image :src="avatar||'../../static/default-avatar.png'" mode="" @click="changeAvatar"></image>
			<view class="basic-info">
				<view class="nickname">
					{{userinfo.userName}}
				</view>
				<view class="username">
					{{userinfo.nickName}}
				</view>
			</view>
		</view>
		<view class="about" @click="jumpAbout">
			<view class="about-left">
				<image src="../../static/write.png" mode=""></image>
				<view class="about-title">
					关于平台
				</view>
			</view>
			<view class="arrow">

			</view>
		</view>
		<view class="btn" @click="logoutFn">
			退出账号
		</view>
	</view>
</template>

<script setup>
	import BASE_URL from "@/config/api.js"
	import {
		logout,
		getInfo
	} from "@/api/index.js"
	import {
		ref,
		computed
	} from "vue";
	const userinfo = ref(null)

	userinfo.value = uni.getStorageSync('userinfo')
	if (!userinfo.value.userName) {
		getInfoFn()
	}
	//头像的计算属性
	const avatar = computed(() => {
		return userinfo.value.avatar ? BASE_URL + userinfo.value.avatar : false
	})
    //获取头像
	const getInfoFn = () => {
		getInfo().then(res => {
			uni.setStorageSync('userinfo', res.user)
			userinfo.value = res.user
		})
	}
	//修改头像
	const changeAvatar = () => {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: (res) => {
				console.log(res);
				uni.uploadFile({
					url: BASE_URL + "/system/user/profile/avatar",
					name: "avatarfile",
					filePath: res.tempFilePaths[0],
					header: {
						'Authorization': 'Bearer ' + uni.getStorageSync('Authorization')
					},
					success: (res) => {
						let data = JSON.parse(res.data)
						if (data.code == 200) {
							getInfoFn()
						} else {
							uni.showToast({
								title: "头像上传失败，请重试！",
								icon: "none"
							})
						}
					},
					fail: (err) => {
						uni.showToast({
							title: "头像上传失败，请重试！",
							icon: "none"
						})
					}
				})
			}
		});
	}
	//退出登录
	const logoutFn = () => {
		uni.showModal({
			title: '系统提示',
			content: '确定退出并注销账号？',
			success: function(res) {
				if (res.confirm) {
					logout().then(res => {
						uni.reLaunch({
							url: "/pages/login/login"
						})
						uni.removeStorageSync('userinfo');
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	//页面跳转
	const jumpAbout=()=>{
		uni.navigateTo({
			url:"/pages/about/about"
		})
	}
</script>

<style lang="scss" scoped>
	.page {
		background-image: url('../../static/hongqi.png');
		background-repeat: no-repeat;
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
	}
	.btn {
		background: $uni-color-primary;
		border-radius: 5px;
		height: 96rpx;
		margin: 40rpx 30rpx 0 30rpx;
		line-height: 96rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: 500;
	}

	.info-box {
		display: flex;
		margin: 64rpx 30rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			margin-right: 16rpx;
		}
	}

	.nickname {
		font-size: 36rpx;
		color: $uni-text-color;
		font-weight: 500;
	}

	.username {
		color: $uni-text-color-grey;
	}

	.basic-info {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 10rpx;
		box-sizing: border-box;
	}

	.about {
		background: #FFFFFF;
		box-shadow: 0px 0px 20rpx rgba(8, 23, 56, 0.05);
		border-radius: 14rpx;
		padding: 28rpx 30rpx;
		margin: 0 30rpx;
		image {
			width: 28rpx;
			height: 28rpx;
		}

		display: flex;
		align-items: center;
		justify-content: space-between;

		.about-title {
			color: $uni-text-color;
			margin-left: 20rpx;
		}

		.about-left {
			display: flex;
			align-items: center;
		}

		.arrow {
			width: 15rpx;
			height: 15rpx;
			border-top: 3rpx solid $uni-text-color-grey;
			border-right: 3rpx solid $uni-text-color-grey;
			transform: rotate(45deg);
		}
	}
</style>